package org.jwap.server.helper;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.Console;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialCommunication{
	
	private InputStream in;
	private OutputStream out;
	
	
    public SerialCommunication()
    {
        super();
    }
    
    public void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();               
            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    
    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream _in;
        
        public SerialReader ( InputStream inStream )
        {
            this._in = inStream;
        }
        
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            boolean isListen = true;
            try
            {
            	 try {
                     Thread.sleep(10);
                 } catch(InterruptedException ex) {
                     Thread.currentThread().interrupt();
                 }
                while ( isListen  &&( len = this._in.read(buffer)) > -1 )
                {
                    String deger = new String(buffer,0,18);
                    if(deger.startsWith("~") && deger.endsWith("\\|")){
                    	isListen = false;
                    	System.out.println("Tx Data: "+deger);
                    }
                    try {
                        Thread.sleep(1000);
                    } catch(InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                    
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream _out;
        String writerString = "";
        
        public SerialWriter ( OutputStream outStream )
        {
            this._out = outStream;
        }
        
        public void run ()
        {
            try
            {
            	 try{
	                 Thread.sleep(1000);
	             } catch(InterruptedException ex) {
	                 Thread.currentThread().interrupt();
	             }
	            this._out.write(writerString.getBytes());
	            System.out.println("System out:"+writerString);
	           
	        }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }
    
    public void listenSerialMessage(){
    	 (new Thread(new SerialReader(in))).start();
    }
    
    public void sendSerialMessage(String msg){
         SerialWriter sw =new SerialWriter(out);
         sw.writerString = msg;
         sw.run();
    }
    
    public static void main ( String[] args )
    {
        try
        {
        	SerialCommunication com = new SerialCommunication();
            (com).connect("COM3");
            com.sendSerialMessage("~LA00000|");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}